package controller;

import com.alibaba.fastjson.JSON;
import entity.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import service.*;
import utils.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Blog")
public class    BlogController {

    @Resource
    private BlogService Blog;
    @Resource
    private LikeService like;

    @Resource
    private UserService user;

    //好友博客方法
    @RequestMapping(value = "/FilrendBlog",produces = {"application/json;charset=UTF-8"})
    public String FilrendBlog(@RequestParam(value = "curPage",defaultValue = "1")Integer curPage,
                              @RequestParam(value = "pageSize",defaultValue = "4") Integer pageSize,
                              Model model, HttpSession session, HttpServletRequest request){
        User user2=(User)session.getAttribute("sos");
        int firendCount= Blog.getFirendCount(user2.getUid());//好友数量
        int count=Blog.getFirendBlogCount(user2.getUid());//数量
//        List<Like> likes=like.getLikeuser(user2.getUid());//取到被点赞的博客
        List<User> users=null;//取到被点赞的用户
        List<User> commentuser=null;//取到评论
        //分页工具类对象
        Page<Blog> page=new Page<Blog>(curPage,pageSize,count);
        //获得集合
        List<Blog> blist=Blog.getFirendBlog(user2.getUid(),page.getBeginPos(),pageSize);
        for (Blog d:blist){
            users=user.getlike(d.getBid());
            d.setLikes(users);
        }
        for (Blog d:blist){
            commentuser=user.getComment(d.getBid());
            d.setComment(commentuser);
        }
        page.setRows(blist);//将集合放入分页工具类
        request.setAttribute("page",page);
        request.setAttribute("count",count);
        request.setAttribute("firendCount",firendCount);
        return "newsfeed";
//        return "redirect:/Directory/newsfeed";
    }
    //显示推荐博客方法
    @RequestMapping("/Bloglist")
    @ResponseBody
    public String Bloglist(HttpServletRequest request, HttpSession session){
////        List<Product> result=service2.getall();
////        String cjson= JSON.toJSONString(result);
//        //产生一百之内随机数
//        Random random = new Random();
//        List<Blog> list=new ArrayList<Blog>();
//        int zhi=Blog.getBlogCount();
//        List<Blog> blogss=Blog.getBlogList();
////        request.setAttribute("blogs",blogss);
//        for (int i=0;i<6;i++){
//            int num = random.nextInt(zhi);//产生一个随机数
//            for (Blog b:blogss) {
//                if(b.getBid()!=num){
//                    i=i-1;
//                }else{
//                    Blog c=Blog.getBlog(num);//把随机数赋值到推荐博客方法中
//                    list.add(c);//添加到list集合里
//                }
//            }
//        }
        List<User> users=null;
        List<Blog> list=Blog.getBlog();
        for (Blog d:list){
            users=user.getlike(d.getBid());
            d.setLikes(users);
        }
        String cjson= JSON.toJSONString(list);
        return cjson;
    }
    //获取自身博客列表
    @RequestMapping("/OneselfBlog")
    @ResponseBody
    public Object OneselfBlog(@RequestParam(value = "is",defaultValue = "1") Integer is,
                              Model model, HttpSession session, HttpServletRequest request) {
        User user2=(User)session.getAttribute("sos");
        List<User> users=null;
        List<Blog> list=Blog.getOneselfBlog(user2.getUid(),is);//显示自己博客量
        for (Blog d:list){
            users=user.getlike(d.getBid());
            d.setLikes(users);
        }
        String cjson= JSON.toJSONString(list);
        return cjson;
    }

    //添加点赞
    @RequestMapping("/tjlike")
    public String tjlike(int blogid,HttpSession session){
        User user2=(User)session.getAttribute("sos");
        int num=like.addLike(user2.getUid(),blogid);
        return "redirect:/Blog/FilrendBlog";
    }

    //取消点赞
    @RequestMapping("/dellike")
    public String dellike(int blogid,HttpSession session){
        User user2=(User)session.getAttribute("sos");
        int num=like.delLike(user2.getUid(),blogid);
        return "redirect:/Blog/FilrendBlog";
    }

}
